package com.example.meituan.activity;
import static com.example.meituan.utils.Constant.WEB_BASE;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.meituan.R;
import com.example.meituan.service.UserService;
import com.example.meituan.utils.UserResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity{
    private ShopActivity.MHandler mHandler;
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(WEB_BASE)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    UserService userService = retrofit.create(UserService.class);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

//        // 设置隐藏状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();//隐藏标题栏
        EditText accountEdit = (EditText) findViewById(R.id.editText1);
        EditText passwordEdit = (EditText) findViewById(R.id.editText2);

        accountEdit.setHint( "账号/手机号/邮箱");
        Button login = (Button) findViewById(R.id.button1);
        Button register = (Button) findViewById(R.id.button2);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();

                // 使用Retrofit进行API调用
                Call<UserResponse> call = userService.loginUser(account, password);
                call.enqueue(new Callback<UserResponse>() {
                    @Override
                    public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            UserResponse userResponse = response.body();
                            int code = userResponse.getCode();
                            if (code == 1) {
                                // 处理登录成功的响应
                                Intent intent = new Intent(LoginActivity.this, ShopActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                String errorMessage = userResponse.getMsg();
                                // 处理登录失败的响应
                                Toast.makeText(LoginActivity.this, errorMessage != null ? errorMessage : "登录失败", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            // 处理请求失败的情况
                            Toast.makeText(LoginActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<UserResponse> call, Throwable t) {
                        Log.e("Retrofit", "请求失败: " + t.getMessage());
                        // 处理API调用失败
                        Toast.makeText(LoginActivity.this, "连接服务器失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        register.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
//        login.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View v) {
//                String account = accountEdit.getText().toString();
//                String password = passwordEdit.getText().toString();
//
//                ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
//                progressDialog.setTitle("登录");
//                progressDialog.setMessage("加载中...");
//                progressDialog.setCancelable(true);
//                progressDialog.show();
//
//                Handler handler = new Handler();
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        progressDialog.cancel();
//                        // 如果账号是admin，且密码是123456，救人位是登陆成功
//                        if(account.equals("") && password.equals("")){
//                            Intent intent = new Intent(LoginActivity.this, ShopActivity.class);
//                            startActivity(intent);
//                            finish();
//                        }
//                        else{
//                            Toast.makeText(LoginActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                }, 3000);//3秒后执行Runnable中的run方法
//
//            }
//        });

    }
    protected long exitTime;//记录第一次点击时的时间
    //点击2次返回键的时间间隔小于2秒钟，退出APP
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(LoginActivity.this, "再按一次退出美团外卖应用",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}


